//
//  SearchProjectViewController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/24/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "SearchProjectViewController.h"
#import "DeptPicker.h"
#import "HRStore.h"
#import "Project.h"
#import "ProjectListController.h"
#import "UIView+Toast.h"

@interface SearchProjectViewController ()

@end

@implementation SearchProjectViewController
{
    int selectedDeptId;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:@"Project" image:[UIImage imageNamed:@"tab_project"] tag:3];
        self.tabBarItem = tab;
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[self navigationItem] setTitle:@"Search Project"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)pickDept:(id)sender {
    CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
    DeptPicker *picker = [[DeptPicker alloc] init];
    [picker setSelectedDeptId:selectedDeptId];
    [picker setSelectedDeptShortName:searchDept.titleLabel.text];
    deptPop = [[UIPopoverController alloc] initWithContentViewController:picker];
    [picker setPopView:deptPop];
    [deptPop setDelegate:self];
    [deptPop setPopoverContentSize:CGSizeMake(300, 600)];
    [deptPop presentPopoverFromRect:rect inView:[self view]
           permittedArrowDirections:UIPopoverArrowDirectionLeft
                           animated:YES];
}

- (IBAction)search:(id)sender
{
    NSArray *result = [[HRStore sharedStore] findProjectAboutName:searchProjectName.text inDept:selectedDeptId];
    
    if (result.count > 0) {
        if (!projectList) {
            projectList = [[UITableView alloc] initWithFrame:contentView.bounds style:UITableViewStylePlain];
            projectController = [[ProjectListController alloc] init];
            [projectController setNc:self.navigationController];
            projectList.dataSource = projectController;
            projectList.delegate = projectController;
            [projectList setBackgroundColor:[UIColor colorWithWhite:0 alpha:0]];
            [contentView addSubview:projectList];
        }
    }
    else {
        [self.view makeToast:@"No matched record."
                    duration:3.0
                    position:[NSValue valueWithCGPoint:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height - 100)]];
    }
    
    [projectController setDataSource:result];
    [projectList reloadData];
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if (popoverController == deptPop) {
        DeptPicker *picker = (DeptPicker *)popoverController.contentViewController;
        [searchDept setTitle:picker.selectedDeptShortName forState:UIControlStateNormal];
        selectedDeptId = picker.selectedDeptId;
    }
     
    [popoverController dismissPopoverAnimated:YES];
    popoverController = nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [searchProjectName resignFirstResponder];
    return YES;
}

@end
